package auth

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreatePermissionLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewCreatePermissionLogic 创建权限
func NewCreatePermissionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePermissionLogic {
	return &CreatePermissionLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreatePermissionLogic) CreatePermission(req *types.CreatePermissionRequest) (resp *types.CreatePermissionResponse, err error) {
	permission, err := l.svcCtx.AuthRoleRpc.CreatePermission(l.ctx, &v1.CreatePermissionRequest{
		PermissionPid:        req.PermissionPid,
		PermissionName:       req.PermissionName,
		PermissionIdentifier: req.PermissionIdentifier,
		ListOrder:            req.ListOrder,
		Status:               v1.Status(req.Status),
	})

	if err != nil {
		return nil, err
	}

	return &types.CreatePermissionResponse{
		Id:                   permission.GetId(),
		PermissionPid:        permission.GetPermissionPid(),
		PermissionName:       permission.GetPermissionName(),
		PermissionIdentifier: permission.GetPermissionIdentifier(),
		ListOrder:            permission.GetListOrder(),
		Status:               int8(permission.GetStatus()),
		CreatedAt:            permission.GetCreatedAt(),
		UpdatedAt:            permission.GetUpdatedAt(),
	}, err
}
